/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.exam;

import java.io.*;

/**
 * <p>Project: javase - Ex5
 * <p>Powered by webrx On 2021-12-15 14:24:41
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex5 {
    public static void main(String[] args) {
        String a = "C:\\Users\\Administrator\\Desktop\\aa.rar";
        String b = "C:\\Users\\Administrator\\Desktop\\cc.rar";
        if (isSame(new File(a), new File(b))) {
            System.out.printf("%s 和 %s 是同一个文件", "aa.rar", "cc.rar");
        } else {
            System.out.printf("%s 和 %s 不是同一个文件", "aa.rar", "cc.rar");
        }
    }

    public static boolean isSame(File a, File b) {
        boolean f = false;
        if (a.length() == b.length()) {
            try (var ia = new FileInputStream(a); var ib = new FileInputStream(b)) {
                long i = 0;
                while (ia.read() == ib.read()) {
                    ++i;
                    if (i == a.length()) break;
                }
                if (ia.read() == -1) f = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }

    public static void test(String[] args) {
        String p = "C:\\Users\\Administrator\\Desktop\\a.txt";
        p = "C:\\javase\\09io\\src\\cn\\exam\\Ex5.java";
        p = "user.png";

        var f = new File(p);
        if (f.exists()) {
            try (var reader = new FileReader(f)) {
                char[] cs = new char[10];
                reader.read(cs);
                System.out.println(new String(cs));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
